﻿var MsAdjust = {};
var TaskId = com.get.TaskId;
var OrgId = com.get.OrgId;
var action = com.get.action;

$(function () {
    var TaskId = com.get.TaskId;
    var OrgId = com.get.OrgId;
    //加载任务信息
    com.ajax({

        url: "/Logic/TM/TaskManagerAjax.aspx?method=GetTaskInfoByPrimaryId&TaskId=" + TaskId, success: function (data) {
            $("#lblTaskCode").html(data.TaskCode);
            $("#lblProjName").html(data.ProjName);
            $("#lblEngName").html(data.EngName);
            $("#lblNum").html(data.TaskNum);
            $("#lblEquipName").html(data.EquipName);
            $("#lblEquipModel").html(data.EquipModel);
            $("#hidEquipId").val(data.EquipId);
            $("#lblXuhao").html(com.get.Xuhao)
        }
    });


    //初始化批量修改界面
    $('#divBatchEdit').dialog({
        title: '批量替换',
        width: 450,
        height: 120,
        collapsible: true,
        minimizable: false,
        closable: true,
        cache: false,
        modal: true,
        buttons: [{
            text: "确定", icon: "icon-save", handler: function () {
                var field = $("#field").combobox("getValue");
                $("#divTbList input[name=" + field + "]").each(function () {
                    var value = $(this).val();
                    var before = $("#before").val();
                    var after = $("#after").val();
                    var s = value.replace(before, after);
                    $(this).val(s);
                });
            }
        }, { text: "取消", handler: function () { $("#divBatchEdit").dialog("close"); } }]
    });
    $("#divBatchEdit").dialog("close");


    var height = $("#divTotal").height();
    var master = $("#master").height();
    var content = parseFloat(height) - parseFloat(master) - 45;
    $("#tbList").css("height", content);


    //加载原始数据列表
    $("#tbList").datagrid({
        url: '/Logic/TM/OrgDataAjax.aspx?method=GetOrgDataByTaskIdOrgId',
        loadMsg: '数据加载中请稍后……',
        queryParams: {
            OrgId: OrgId,
            TaskId: TaskId
        },
        rownumbers: true,
        idField: "ID",
        singleSelect: true,
        autoRowHeight: false,
        columns: [[{ field: 'ck', checkbox: true, width: 30 },
              {
                  field: 'MsXuhao', title: '明细序号', align: "center", width: 90, formatter: function (value, row, Index) {
                      return "<input id='MsXuhao' name='MsXuhao'   value='" + value + "'  style='width:80px' />";
                  }
              },
               {
                   field: 'Xuhao', title: '序号', align: "center", width: 100, formatter: function (value, row, Index) {
                       return "<input id='Xuhao' name='Xuhao'    value='" + value + "'  style='width:90px' />";
                   }
               },
                             {
                                 field: 'Map', title: '图号', align: "center", width: 200, formatter: function (value, row, Index) {
                                     return "<input id='Map' name='Map'   value='" + value + "'  style='width:180px' />";
                                 }
                             },
                             {
                                 field: 'Zongxu', title: '总序', align: "center", width: 100, formatter: function (value, row, Index) {
                                     return "<input id='Zongxu' name='Zongxu'   value='" + value + "'  style='width:90px' />";
                                 }
                             },
                             {
                                 field: 'MaterialName', title: '名称', align: "center", width: 140, formatter: function (value, row, Index) {
                                     return "<input id='MaterialName' name='MaterialName'    value='" + value + "'  style='width:130px' />";
                                 }
                             }, {
                                 field: 'IsSubmitMs', title: '体现', align: "center", width: 60, formatter: function (value, row, Index) {
                                     return "<input id='IsSubmitMs' name='IsSubmitMs'  value='" + value + "'  style='width:50px' />";
                                 }
                             },
                             {
                                 field: 'Ku', title: 'K ', align: "center", width: 50, formatter: function (value, row, Index) {

                                     return "<input id='Ku' name='Ku'  value='" + value + "'  style='width:40px' />";
                                 }
                             },
                             {
                                 field: 'MaterialGuige', title: '规格', align: "center", width: 120, formatter: function (value, row, Index) {
                                     return "<input id='MaterialGuige' name='MaterialGuige'    value='" + value + "'  style='width:110px'  />";
                                 }
                             }, {
                                 field: 'MaterialCaizhi', title: '材质', align: "center", width: 120, formatter: function (value, row, Index) {
                                     return "<input id='MaterialCaizhi' name='MaterialCaizhi'    value='" + value + "'  style='width:110px' readonly='readonly' />";
                                 }
                             }, { field: 'Num', title: '数量 ', align: "center", width: 40 },

                   {
                       field: 'Process', title: '工艺流程 ', align: "center", width: 90, formatter: function (value, row, Index) {

                           return "<input id='Process' name='Process'  value='" + value + "'  style='width:80px' />";
                       }
                   },
                                      {
                                          field: 'BlankType', title: '下料类别 ', align: "center", width: 90, formatter: function (value, row, Index) {

                                              return "<input id='BlankType' name='BlankType'  value='" + value + "'  style='width:80px' />";
                                          }
                                      },
          {
              field: 'Note', title: '备注', align: "center", width: 280, formatter: function (value, row, Index) {

                  return "<input id='Note' name='Note'    value='" + value + "'  style='width:260px' />";
              }
          },
       //  { field: 'MaterialCaizhi', title: '材质 ', align: "center", width: 100 },
        // { field: 'UnitWeight', title: '单重(kg) ', align: "center", width: 70 },
        // { field: 'MaterialGB', title: '国标', align: "center", width: 60 },
       //  { field: 'HidGuige', title: '材料规格', align: "center", width: 100 },
       //  { field: 'MaTotalWeight', title: '材料总重', align: "center", width: 70 },
       //  { field: 'TotalLength', title: '材料总长', align: "center", width: 60 },
       //  { field: 'PlanArea', title: '计划面域', align: "center", width: 70 },
      //   { field: 'MaState', title: '毛胚状态 ', align: "center", width: 60 },
       //  { field: 'IsFixedSize', title: '是否定尺 ', align: "center", width: 60 },
        ]], onLoadSuccess: function (data) {
            $("input[name=Ku]").combobox({
                data: StaticCombo.Ku,
                valueField: 'value',
                textField: 'text',
                panelHeight: "auto",
                editable: true,
                onSelect: function (rec) {
                    var pro = rec.value;
                    if (pro == 'K') {
                        $(this).closest("tr").find("#IsSubmitMs").combobox("select", "Y");
                    }
                }
            });

            $("input[name=IsSubmitMs]").combobox({
                data: StaticCombo.IsElect,
                valueField: 'value',
                textField: 'text',
                panelHeight: "auto",
            });

            $("input[name=BlankType]").combobox({
                data: StaticCombo.BlankType,
                valueField: 'value',
                textField: 'text',
                panelHeight: "auto",
            });

            $("input[name=Process]").combobox({
                data: StaticCombo.Process,
                valueField: 'value',
                textField: 'text',
                panelHeight: "auto",
                editable: true,
                onSelect: function (rec) {

                    var pro = rec.value;
                    if (pro == '0' || pro == '1' || pro == '2' || pro == '3') {
                        $(this).closest("tr").find("#IsSubmitMs").combobox("select", "Y");
                    }
                }
            });

            $("#divTbList input").keydown(function () {
                commonKeydown($(this));
            });
            $("#divTbList input").click(function () {
                $(this).select();
            });
            $("#divTbList input").keyup(function () {
                if (event && event.keyCode && (event.keyCode == 38 || event.keyCode == 40 || event.keyCode == 37 || event.keyCode == 39)) {
                    $(this)[0].select();
                }
                else {
                    if (this.id == "Xuhao") {
                        var XuhaoModel = $("#XuhaoModel").combobox("getValue");

                        if (XuhaoModel == "0") {

                            $(this).closest("tr").find("#Zongxu").val($(this).val());

                        }
                    }
                }

            });
        }
    });


});


MsAdjust.Search = function () {

    var o = {
        OrgId: OrgId,
        TaskId: TaskId
    };
    $("#tbList").datagrid("load", $.extend(com.serializeSearchArray("form1"), o));

}

function commonKeydown($input) {
    if (event && event.keyCode && (event.keyCode == 38 || event.keyCode == 40 || event.keyCode == 37 || event.keyCode == 39)) {
        if ((event.keyCode == 38 || event.keyCode == 40)) {
            var row = $("#tbList").datagrid("getSelected");
            var rows = $("#tbList").datagrid("getRows");
            var rowIndex = $("#tbList").datagrid("getRowIndex", row)
            if (event.keyCode == 38) {
                if (rowIndex != 0) {
                    $("#tbList").datagrid("selectRow", rowIndex - 1);
                    $input.closest("tr").prev().find("#" + $input[0].id).focus();
                    //   $input.closest("tr").prev().find("#" + $input[0].id).select();
                }

            }
            else if (event.keyCode == 40) {
                if (rowIndex != rows.length - 1) {
                    $("#tbList").datagrid("selectRow", rowIndex + 1);
                    $input.closest("tr").next().find("#" + $input[0].id).focus();
                    //    $input.closest("tr").next().find("#" + $input[0].id).select();
                }

            }

        }
        else if (event.keyCode == 37 || event.keyCode == 39) {
            var $td = $input.closest("td");
            if (event.keyCode == 37) {
                while (true) {
                    var $prevTd = $td.prev();
                    if ($prevTd.length > 0) {
                        var prevInput = $prevTd.find("input");
                        if (prevInput.length == 0) {
                            $td = $prevTd;
                        }
                        else {
                            //for (var i = 0; i < prevInput.length; i++) {
                            //    if ($(prevInput[i]).css("display") != "none") {
                            //        $(prevInput[i]).focus();
                            //        //  prevInput.select();
                            //        // $prevTd.find("input").select();
                            //        return;
                            //    }

                            //}
                            if (prevInput.css("display") != "none") {
                                prevInput.focus();
                                break;
                            }
                            else {
                                $td = $prevTd;
                            }
                        }
                    }
                    else {
                        break;
                    }
                }
            }
            else if (event.keyCode == 39) {
                while (true) {
                    var $nextTd = $td.next();
                    if ($nextTd.length > 0) {
                        var nextInput = $nextTd.find("input");
                        if (nextInput.length == 0) {
                            $td = $nextTd;
                        }
                        else {
                            //for (var i = 0; i < nextInput.length; i++) {
                            //    if ($(nextInput[i]).css("display") != "none") {
                            //        $(nextInput[i]).focus();
                            //        //   nextInput.select();
                            //        // $prevTd.find("input").select();                                
                            //        return;
                            //    }
                            //}
                            if (nextInput.css("display") != "none") {
                                nextInput.focus();
                                break;
                            }
                            else {
                                $td = $nextTd;
                            }
                        }
                    }
                    else {
                        break;
                    }

                }
            }
        }


        if (event && event.keyCode && event.keyCode == 40) {
            var input = $input[0];
            if (input.id == "Xuhao") {
                if ($input.closest("tr").next().find("#Xuhao").val() == "") {
                    var lastXuhao = $input.val();
                    var newXuhao = "";
                    var autoXuhao = $("#autoXuhao").combobox("getValue");
                    if (autoXuhao == "0") {

                        if (lastXuhao.lastIndexOf(".") == -1) {
                            newXuhao = utils.ParseFloat(lastXuhao) + 1;
                        }
                        else {
                            var fXuhao = lastXuhao.substring(0, lastXuhao.lastIndexOf("."));
                            var subXuhao = lastXuhao.split('.')[lastXuhao.split('.').length - 1];
                            newXuhao = fXuhao + '.' + (utils.ParseFloat(subXuhao) + 1);

                        }
                        $input.closest("tr").next().find("#Xuhao").val(newXuhao);
                    }
                    else if (autoXuhao == "1") {
                        var frontXuhao = $("#frontXuhao").val()
                        var newXuhao = frontXuhao + "." + lastXuhao;
                        $input.closest("tr").find("#Xuhao").val(newXuhao);
                    }


                    var XuhaoModel = $("#XuhaoModel").combobox("getValue");

                    if (XuhaoModel == "0") {
                        $input.closest("tr").find("#Zongxu").val(newXuhao);


                    }
                }
            }
            if (input.id == "MsXuhao" && $("#autoMsXuhao").combobox("getValue") == "0") {
                var lastXuhao = $input.val();
                var newXuhao = "";
                if (lastXuhao.lastIndexOf(".") == -1) {
                    newXuhao = utils.ParseFloat(lastXuhao) + 1;
                }
                else {
                    var fXuhao = lastXuhao.substring(0, lastXuhao.lastIndexOf("."));
                    var subXuhao = lastXuhao.split('.')[lastXuhao.split('.').length - 1];
                    newXuhao = fXuhao + '.' + (utils.ParseFloat(subXuhao) + 1);

                }
                $input.closest("tr").next().find("#MsXuhao").val(newXuhao);
            }
            if (input.id == "Map") {
                if ($input.closest("tr").next().find("#Map").val() == "") {
                    if ($("#autoMap").combobox("getValue") == "1") {
                        var lastMap = $input.val();
                        $input.closest("tr").next().find("#Map").val(lastMap);
                    }
                    else if ($("#autoMap").combobox("getValue") == "2") {
                        var frontMap = $("#frontMap").val();
                        var lastMap = $input.val();

                        var num = lastMap.replace(frontMap, "");
                        var newMap = frontMap + (utils.ParseInt(num) + 1);
                        $input.closest("tr").next().find("#Map").val(newMap);
                    } else if ($("#autoMap").combobox("getValue") == "3") {
                        var frontMap = $("#frontMap").val();
                        var lastMap = $input.val();
                        var newMap = frontMap + lastMap;
                        $input.closest("tr").find("#Map").val(newMap);
                    }
                }
   

                //     var newXuhao = utils.ParseInt(lastMsXuhao) + 1;

            }
        }
    }

}

MsAdjust.SaveOrgData = function () {
    var array = new Array();
    var rows = $("#tbList").datagrid("getRows");
    for (var i = 0; i < rows.length; i++) {
        var $tr = $("#divTotal tr:eq(" + (parseInt(i + 1) + parseInt(rows.length) + 1) + ")");
        rows[i].MsXuhao = $tr.find("#MsXuhao").val();
        rows[i].Xuhao = $tr.find("#Xuhao").val();
        rows[i].Map = $tr.find("#Map").val();
        rows[i].Zongxu = $tr.find("#Zongxu").val();
        rows[i].MaterialName = $tr.find("#MaterialName").val();
        rows[i].MaterialGuige = $tr.find("#MaterialGuige").val();
        rows[i].IsSubmitMs = $tr.find("#IsSubmitMs").combobox("getValue");
        rows[i].Ku = $tr.find("#Ku").combobox("getValue");
        rows[i].Process = $tr.find("#Process").combobox("getValue");
        rows[i].BlankType = $tr.find("#BlankType").combobox("getValue");
        rows[i].Note = $tr.find("#Note").val();
        array.push(rows[i]);
    }

    com.ajax({
        url: "/Logic/TM/OrgDataAjax.aspx?method=AdjustMsData", data: { data: JSON.stringify(array), TaskId: TaskId, CopySwitch: $("#CopySwitch").html(), Xuhao: $("#lblXuhao").html(), OrgId: com.get.OrgId }, success: function (data) {
            com.message(data.state, data.msg);
            if (data.state == "success") {
                $("#tbList").datagrid("reload");
            }
        }
    });
}

MsAdjust.BatchEdit = function () {

    $("#divBatchEdit").dialog("open");
}
MsAdjust.ClearXuhao = function () {
    $("input[name=Xuhao]").val("");

}

//切换复制开关
MsAdjust.EditCopySwitch = function () {
    if ($("#CopySwitch").html() == "开") {

        $("#CopySwitch").html("关");
    }
    else {

        $("#CopySwitch").html("开");
    }

}